<?php require_once('../connections/con_gl.php'); ?>
<?php
$act = $_GET[act];
$url = "index.php?component=kinvoice";

switch($act){
	default :
		if (trim($_POST['kontrak']) == '') {
			$error[] = '- kontrak harus dipilih !!!';
		}
		if (isset($error)) {
			echo "<img src=\"images/alert.png\" width=\"16\" align=\"left\"/>&nbsp;&nbsp;<b style=\"color:red;\">Error : </b> <br />".implode("<br />", $error);
		}else{
		// -- insert rtrans
		$query_insert_1 = "insert into gl_retailtrans (no_ref,jnsorder,tgl,keterangan,kontak,jenis,gudang,area,jatuhtempo,kontrak) values ('$_POST[noreferensi]','1','$_POST[tglawal]','$_POST[keterangan]','$_POST[dari]','$_POST[jenis]','$_POST[gudang]','$_POST[area]','$_POST[tglakhir]','$_POST[kontrak]')";
		print($query_insert_1);
		
		// hitung kendaraan
		$ken  = $_POST['kendaraan'];
		$sat  = $_POST['satuan'];
		$jum  = $_POST['jumlah'];
		$har  = $_POST['hargasatuan'];
		$dis  = $_POST['diskon'];
		$tot  = $_POST['total'];
		$ppn = $_POST['pajak'];
		$tra  = $_POST['transaksi'];
		$akn  = $_POST['akun_jual'];
		$bar  = $_POST['barang'];
		$bar  = '24';
		$jken = count($ken);
		//--- gen query tran
		// ppn belum masuk
		$query_insert_2 = "insert into gl_detailtrans(id,no_ref,transaksi,total,barang,kendaraan,ppn, hargasatuan,diskon) values ";
		for($i=0;$i<$jken;$i++) {
			$query_insert_2 .= "('','$_POST[noreferensi]','$tra[$i]','$tot[$i]','$bar[$i]','$ken[$i]','$ppn[$i]','$har[$i]','$dis[$i]')";
			echo 'done';
			print($query_insert_2);
			if($i < $jken-1) {
				$query_insert_2 .=",";	
			}else{
				$query_insert_2 .=";";
			}
		}
		// save data
		mysql_select_db($database_con_gl, $con_gl);
		$insert_1 = mysql_query($query_insert_1, $con_gl) or die(mysql_error());
		if(insert_1) {
			//--- save transaksi
			mysql_select_db($database_con_gl, $con_gl);
			$insert_2 = mysql_query($query_insert_2, $con_gl) or die(mysql_error());
			// --
					
			
			
			echo "<img src=\"images/ok.png\" align=\"left\" width=\"16\">&nbsp;&nbsp;Generated Invoice berhasil ...";
		}else{
			echo "<img src=\"images/alert.png\" align=\"left\" width=\"16\"> Generated Invoice gagal !!!";	
		}
		}
		break;
	
	case 'delete' :
		$noref = $_REQUEST['noreferensi'];
		//Hapus gl_trans
		mysql_query("delete from gl_rtrans where no_ref = '$noref'") or die (mysql_error());
		mysql_query("delete from gl_trans where no_ref = '$noref'") or die (mysql_error());
		echo "<meta http-equiv='refresh' content='0;url=../index.php?component=kinvoice' />";
		break;
}
?>
<?php if(insert_1) { ?><script type="text/javascript">setTimeout("location.href='<?php echo $url;?>'", 2000);</script><?php } ?>